package com.xl.basic.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.Accessors;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 省份城市工具
 *
 * @author 钟勇
 * Create in 2023/9/12 14:06
 */
@Component
public class AreaUtils {

    @Getter
    private static List<AreaInfo> areaList;

    @PostConstruct
    public void init() {
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("area.json")) {
            try {
                areaList = JacksonUtils.objectMapper().readValue(IoUtil.readUtf8(is), new TypeReference<>() {
                });
            } catch (JsonProcessingException e) {
                areaList = new ArrayList<>();
            }
        } catch (IOException e) {
            areaList = new ArrayList<>();
        }


    }


    /**
     * 获取省份列表
     *
     * @return list
     */
    public static List<ProvinceInfo> queryProvinceList() {
        return areaList.stream()
                .filter(item -> item.getCityId() == null).map(item -> (ProvinceInfo) item)
                .toList();
    }

    /**
     * 获取城市列表
     *
     * @param provinceId 省份id
     * @return list
     */
    public static List<CityInfo> queryCityListByProvinceId(@NonNull Integer provinceId) {
        return areaList.stream()
                .filter(item -> item.getCityId() != null && item.getAreaId() == null && item.getProvinceId().equals(provinceId))
                .map(item -> (CityInfo) item)
                .toList();
    }

    /**
     * 获取区域列表
     *
     * @param cityId 城市id
     * @return list
     */
    public static List<AreaInfo> queryAreaListByCityId(@NonNull Integer cityId) {
        return areaList.stream()
                .filter(item -> item.getAreaId() != null && item.getCityId().equals(cityId))
                .toList();
    }


    /**
     * 获取省份信息
     *
     * @param provinceId 省份id
     * @return info
     */
    public static ProvinceInfo getProvinceInfo(@NonNull Integer provinceId) {
        return areaList.stream()
                .filter(item -> item.getCityId() == null && item.getProvinceId().equals(provinceId))
                .findFirst()
                .orElse(null);
    }

    /**
     * 获取城市信息
     *
     * @param cityId 城市id
     * @return info
     */
    public static CityInfo getCityInfo(@NonNull Integer cityId) {
        return areaList.stream()
                .filter(item -> item.getCityId() != null && item.getAreaId() == null && item.getCityId().equals(cityId))
                .findFirst()
                .orElse(null);
    }

    /**
     * 获取区域id
     *
     * @param areaId 区域id
     * @return info
     */
    public static AreaInfo getAreaInfo(@NonNull Integer areaId) {
        return areaList.stream()
                .filter(item -> item.getAreaId() != null && item.getAreaId().equals(areaId))
                .findFirst()
                .orElse(null);
    }


    public static AreaInfo getAreaInfoByAnyId(@NonNull Integer id) {
        return areaList.stream()
                .filter(item -> {
                    if (item.getAreaId() != null && item.getAreaId().equals(id)) {
                        return true;
                    }

                    if (item.getAreaId() == null && item.getCityId() != null && item.getCityId().equals(id)) {
                        return true;
                    }

                    return item.getAreaId() == null && item.getCityId() == null && item.getProvinceId().equals(id);
                })
                .findFirst()
                .orElse(null);
    }


    @Data
    @Accessors(chain = true)
    public static class ProvinceInfo {
        private Integer provinceId;
        private String provinceName;
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @Accessors(chain = true)
    public static class CityInfo extends ProvinceInfo {
        private Integer cityId;
        private String cityName;
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @Accessors(chain = true)
    public static class AreaInfo extends CityInfo {
        private Integer areaId;
        private String areaName;
    }

}
